package com.magist.manage.copyrights.controller;

import com.github.pagehelper.PageInfo;
import com.magist.core.controller.BaseController;
import com.magist.core.entity.ResultMessage;
import com.magist.core.exception.BusinessException;
import io.swagger.annotations.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;
import tk.mybatis.mapper.entity.Example;


import com.magist.manage.copyrights.entity.ContractBalance;
import com.magist.manage.copyrights.dto.ContractBalanceDto;
import com.magist.manage.copyrights.service.ContractBalanceService;

@ApiIgnore
@RestController
@RequestMapping("/rest/contractBalance")
public class RestContractBalanceController extends BaseController {

	@Autowired
	private ContractBalanceService contractBalanceService;

	@ApiOperation(value="保存", notes="保存接口")
	@RequestMapping(value = "save.do", method = RequestMethod.POST)
	public Object save(@ModelAttribute("contractBalanceDto") ContractBalanceDto contractBalanceDto) {
		ContractBalance contractBalance = new ContractBalance();
		BeanUtils.copyProperties(contractBalanceDto,contractBalance);
		contractBalance.setId(contractBalanceService.getNewId());
		contractBalanceService.insert(contractBalance);
		return ResultMessage.success();
	}

	@ApiOperation(value="更新", notes="更新接口")
	@RequestMapping(value = "update.do", method = RequestMethod.POST)
	public Object update(@ModelAttribute("contractBalanceDto") ContractBalanceDto contractBalanceDto) {
		ContractBalance oldContractBalance = contractBalanceService.getById(contractBalanceDto.getId());
		if(oldContractBalance==null)
			throw new BusinessException("无效的数据!");
		ResultMessage result =null;
		ContractBalance contractBalance = new ContractBalance();
		BeanUtils.copyProperties(contractBalanceDto,contractBalance);
		contractBalance.setModifyTime(oldContractBalance.getModifyTime());
		if (contractBalanceService.update(contractBalance)>0) 
			result = ResultMessage.success("操作成功");
		else
			result=ResultMessage.error("操作失败!");
		return result;
	}

	@ApiOperation(value="列表", notes="分页查询接口")
	@RequestMapping(value = "page.do", method = RequestMethod.GET)
	public Object page(@ModelAttribute("contractBalanceDto") ContractBalanceDto contractBalanceDto) {
		ResultMessage result = ResultMessage.success();
		Example example = new Example(ContractBalance.class);
		example = packExample(example,contractBalanceDto);
		PageInfo<ContractBalance> pageInfo = contractBalanceService.page(example,getPageInfo());
		result.setContent(pageInfo);
		return result;
	}

	@ApiOperation(value="删除", notes="删除接口")
	@RequestMapping(value = "del.do", method = RequestMethod.POST)
	public Object del(@ModelAttribute("id") String id) {
		ContractBalance contractBalance = contractBalanceService.getById(id);
		if(contractBalance==null)
			throw new BusinessException("无效的数据!");
		ResultMessage result =null;
		if(contractBalanceService.delete(contractBalance)>0)
			result = ResultMessage.success("操作成功");
		else
			result=ResultMessage.error("操作失败!");
		return result;
	}

	@ApiOperation(value="详情", notes="详情接口")
	@RequestMapping(value = "info.do", method = RequestMethod.GET)
	public Object info(@ModelAttribute("id") String id) {
		ContractBalance contractBalance = contractBalanceService.getById(id);
		if(contractBalance==null)
			throw new BusinessException("无效的数据!");
		ContractBalanceDto contractBalanceDto = new ContractBalanceDto();
		BeanUtils.copyProperties(contractBalance,contractBalanceDto);
		ResultMessage result = ResultMessage.success();
		result.setContent(contractBalanceDto);
		return result;
	}

	private Example packExample(Example example,ContractBalanceDto contractBalanceDto){
		Example.Criteria criteria = example.createCriteria();
		criteria.andEqualTo("deleted","F");
		if(StringUtils.isNotEmpty(contractBalanceDto.getId()))
			criteria.andEqualTo("id",contractBalanceDto.getId());
		return example;
	}
}
